import {
  mapGetters,
  mapActions
} from 'vuex'
import BMap from 'BMap';
export default {
  data() {
    return {
      currentCity: ''
    }
  },
  computed: {
    ...mapGetters(['addr'])
  },
  mounted() {
    if (this.addr.longitude === '' || !this.addr.longitude) {
      this.getLocation()
    }
  },
  methods: {
    ...mapActions(['initAddr']),
    getLocation() {
      //   const myCity = new BMap.LocalCity();
      //   myCity.get(result => {
      //     this.currentCity = result.name;
      //     this.userAddr.currentCity = result.name;
      //     this.initAddr(this.userAddr);
      //   });
      // 经纬度
      const geolocation = new BMap.Geolocation();
      geolocation.getCurrentPosition(r => {
        if (r.point) {
          this.userlng = r.point.lng;
          this.userlat = r.point.lat;
          this.userAddr = {
            longitude: this.userlng,
            latitude: this.userlat
          };
          this.initAddr(this.userAddr);
        } else { //定位失败重复发起定位
          this.getLocation()
        }
      }, {
        enableHighAccuracy: true
      });
    }
  }
}
